import logging
import requests


class CityService:
    @staticmethod
    def get_city_by_ip(ip: str) -> dict|None:
        """
        根据IP地址获取城市信息
        """
        try:
            response = requests.get(f"https://whois.pconline.com.cn/ipJson.jsp?json=true&ip={ip}")
            response.raise_for_status()
            data = response.json()
            if data:
                return {"city":data.get("city"),"cityCode":data.get("cityCode"),"pro":data.get("pro"),"proCode":data.get("proCode")}
        except Exception as e:
            logging.error(f"Error getting city by IP: {str(e)}")
        return {}